﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using groupAnalyzer.utils;

namespace groupAnalyzer.model
{
    [Serializable]
    public class Settings
    {
        private static Settings settingObject;

        private bool savePassword;

        public bool SavePassword
        {
            get { return savePassword; }
            set { savePassword = value; }
        }

        private string baseDN;

        public string BaseDN
        {
            get { return this.baseDN; }
            set { baseDN = value; }
        }
        private string ldapURL;

        public string LdapURL
        {
            get { return ldapURL; }
            set { ldapURL = value; }
        }
        private string username;

        public string Username
        {
            get { return username; }
            set { username = value; }
        }
        private string password;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        private AuthModeEnum authMode;

        public AuthModeEnum AuthMode
        {
            get { return authMode; }
            set { authMode = value; }
        }

        private void init(string baseDN, string ldapURL, string username, string password, AuthModeEnum authMode)
        {
            this.baseDN = baseDN;
            this.ldapURL = ldapURL;
            this.username = username;
            this.password = password;
            this.authMode = authMode;

            Settings.settingObject = this;
        }

        public void saveSettings()
        {
            Serializer.ObjectToXml(this, "config.xml");
        }

        public Settings(string baseDN, string ldapURL, string username, string password)
        {
            this.init(baseDN, ldapURL, username, password, AuthModeEnum.usernamePassword);
        }

        public Settings(string baseDN, string ldapURL)
        {
            this.init(baseDN, ldapURL, null, null, AuthModeEnum.kerberos);
        }

        public Settings()
        {

        }

        public static void loadSettings()
        {
            if (!System.IO.File.Exists("config.xml"))
            {
                Settings.settingObject = new Settings();
                settingObject.saveSettings();
                return;
            }
            try
            {
                Settings.settingObject = (Settings)Serializer.XmlToObject(typeof(Settings), "config.xml");
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Settings getSettings()
        {
            return Settings.settingObject;
        }
    }
}
